package pers.litao.bit.product.controller;

import org.springframework.web.bind.annotation.*;
import pers.litao.bit.commons.entities.priduct.GoodsEntity;
import pers.litao.bit.commons.utils.R;
import pers.litao.bit.commons.utils.SysUtil;
import pers.litao.bit.product.service.GoodsService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * @author 李涛
 * @date 2020-11-25 14:48
 */
@RestController
@RequestMapping("/product/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    /**
     * 保存商品
     *
     * @param goodsEntity 实体类
     * @return R
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public R saveGoods(@RequestBody GoodsEntity goodsEntity,HttpServletRequest request){
        return goodsService.saveGoods(goodsEntity, SysUtil.getLoginUserInfo(request));
    }

    /**
     * 查询商品
     *
     * @param id 商品id
     * @return R
     */
    @RequestMapping("/{id}")
    public R getGoodsById(@PathVariable("id") Long id){
        return goodsService.getGoodsById(id);
    }

    /**
     * 爬取天猫商品
     *
     * @param key 关键字
     * @return R
     */
    @RequestMapping(value = "/gettmallgoods",method = RequestMethod.GET)
    public R getTmallGoods(@RequestParam("key") String key){

        return R.ok();
    }

    /**
     * 获取热门商品
     *
     * @return R
     */
    @RequestMapping(value = "/gethotgoods")
    public R getHotGoods(HttpServletRequest request){
        return goodsService.getHotGoods(SysUtil.getLoginUserInfo(request));
    }

    /**
     * 获取最新商品
     *
     * @return R
     */
    @RequestMapping(value = "/getnewgoods")
    public R getNewGoods(HttpServletRequest request){
        return goodsService.getNewGoods(SysUtil.getLoginUserInfo(request));
    }

    /**
     * 获取商品列表
     *
     * @return R
     */
    @RequestMapping("/list")
    public R getGoodsList( @RequestParam Map<String,Object> params,HttpServletRequest request){
        return goodsService.getGoodsList(params,SysUtil.getLoginUserInfo(request));
    }


    /**
     * 获取用户商品列表
     *
     * @param page 查询第几页
     * @param httpServletRequest 获取token
     * @return R
     */
    @RequestMapping("/usergoodslist/{page}")
    public R userGoodsList(@PathVariable("page") Long page, HttpServletRequest httpServletRequest){
        return goodsService.userGoodsList(page,SysUtil.getLoginUserInfo(httpServletRequest));
    }

    /**
     * 根据商品id删除商品
     *
     * @param id 商品id
     * @param httpServletRequest 获取token
     * @return R
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public R deleteById(@PathVariable("id") Long id, HttpServletRequest httpServletRequest){
        return goodsService.deleteById(id,SysUtil.getLoginUserInfo(httpServletRequest));
    }

    /**
     * 商品详细处的猜你喜欢
     *
     * @param id 商品id
     * @return r
     */
    @RequestMapping(value = "/detailed/like/{id}",method = RequestMethod.GET)
    public R detailedGuessYouLike(@PathVariable("id") Long id,HttpServletRequest request){
        return goodsService.detailedGuessYouLike(id,SysUtil.getLoginUserInfo(request));
    }

    /**
     * 批量查询商品列表，内部调用
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/goodsList/batch")
    public R getGoodsListBatch(@RequestBody Long[] ids){

        return goodsService.getGoodsListByIds(ids);
    }

    /**
     * 获取商品数量
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/count")
    public R getGoodsCount(HttpServletRequest request){
        return goodsService.getGoodsCount(SysUtil.getLoginUserInfo(request));
    }

}
